//logs.js
const util = require('../../utils/util.js')
import { login } from '../../lib/server'

Page({
  data: {
    logs: [],
    mobile:'',
    password:''
  },
  onLoad: function () {
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map(log => {
        return util.formatTime(new Date(log))
      })
    })
  },
  bindKeyInput: function(e) {
    console.log(e)
    this.setData({
      mobile:e.detail.value
    })
  },
  bindKeyInputPwd: function (e){
    console.log(e)
    this.setData({
      password:e.detail.value
    })
  },
  userLogin: function (){
    if(!this.data.mobile || !this.data.password){
      wx.showToast({
        title: '请完善信息',
      })
      return
    }
    if(this.data.mobile.length!=11){
      wx.showToast({
        title: '手机号错误',
        icon:'success',
        image:'../../image/icon_mypage_yiquxiao.png'
      })
      return
    }
    login({
      mobile:this.data.mobile,
      password:this.data.password
    },{
      success:data => {
        wx.setStorageSync('token', "Bearer " + data.token)
        wx.setStorageSync('user', data.user)
        wx.switchTab({url:"../center/center"})
      }
    })
  }
})
